var app = getApp();
Page({

  data: { 
    windowWidth: 330,
    heightStyle: 445,
    qrCode: '',   //小程序二维码
    imgUrl: '',   //海报url
    index: 1,     //海报样式索引,默认第一个
    template: ''  //模板类型: distributor 分销码, platform 平台首页码
  },
  
  onLoad: function (e) {
    let that = this;
    let windowWidth = wx.getSystemInfoSync().windowWidth * 0.88;
    let heightStyle = windowWidth * 1.5;
    let qrCode = e.qrCode ? e.qrCode : '';
    let template = e.template ? e.template : '';
    that.setData({
      windowWidth: windowWidth,
      heightStyle: heightStyle,
      qrCode: qrCode,
      template: template
    })
    if(template=='platform'){
      wx.setNavigationBarTitle({title: '分享二维码'});
    }
    if(e.qrCode){      
      that.getPoster(that.data.qrCode);   //用传参的二维码生成海报
    }else{
      this.getAiCardInfo();     //用名片首月生成海报
    }    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { 
  },  

   //当前用户名片
   getAiCardInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            that.data.cardInfo = res.data.Data;
            that.getWxaCodeFile(res.data.Data);
          }
        }
      },
    })
  },
//获取小程序二维码
getWxaCodeFile(cardInfo) {
  var that = this;
  var path = 'pages/index/index?t=1&p=' + cardInfo.Id + '#2';
  wx.request({
    url: app.globalData.host + '/api/Store/GetWxaCodeFile',
    data:{
      storeId: app.globalData.storeId,
      path: path,
      storeAppId: app.globalData.storeAppId
    },
    success: function (res) {
      if (res.data.Code == 0) {
        that.getPoster(res.data.Data);          
      }       
    }
  })
},
  //切换样式
  changeStyle: function () {
    this.getPoster(this.data.qrCode);
  },
  saveMerPost: function () {
    var that = this;
    wx.downloadFile({
      url: that.data.imgUrl,
      success: function (res) {
        let tempFilePath = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(response) {        
            wx.showModal({
              content: '图片已保存到相册，赶紧晒一下吧~',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#333',
              success: function (res) {
                if (res.confirm) { }
              },
              fail: function (res) { }
            })
          },
          fail: function (res) {
            console.log('保存失败',res);
            wx.showToast({
              title: '请删掉小程序再重新授权允许保存至相册',
              icon: 'none',
              duration: 3000
            })
          }
        })

      }
    })
    
  },
  //生成海报
  getPoster: function (qrCode){
    let that = this;
    let index = that.data.index;  
    if(!qrCode){
      wx.showToast({
        title: '小程序码加载失败,请重新进入页面',
        icon: 'none',
      })  
      return;
    }    
    wx.showLoading({title: '生成中...',mask: true})
    wx.request({
      url: app.globalData.host + '/api/Distributor/getPoster?Uid=' + app.globalData.userInfo.Uid +'&index=' + index + '&qrCode=' + qrCode + '&template=' + that.data.template,
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0 && res.data.Data) {
            let data = res.data.Data;
            index = index >= data.TotalCount ? 1 : index + 1;
            that.setData({
              qrCode: qrCode,
              imgUrl: data.FileName,
              index: index
            })
        }else {
          wx.showToast({
            title: '生成失败，请稍后再试',
            icon: 'none',
          })
        }
      },
    })
  },

})